﻿using MobilesStore.Domain.Abstract;
using MobilesStore.Domain.Entities;
using MobilesStore.WebUI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MobilesStore.WebUI.Controllers
{
    public class SearchController : Controller
    {
        public int PageSize = 16;
        private IProductsRepository ProductsRepository;

        public SearchController(IProductsRepository repo)
        {
            this.ProductsRepository = repo;
        }

        public ActionResult Index()
        {
            Search _search = new Search
            {
                SearchOptions = new[]{
                    new Option{Description="1tr - 5tr",Id=1},
                    new Option{Description="6tr - 10tr",Id=2},
                    new Option{Description="11tr - 15tr",Id=3},
                    new Option{Description="16tr - 20tr",Id=4}
                }
            };
            return View(_search);
        }

        [HttpPost]
        public ActionResult Index(Search _search, int page = 1)
        {
            if (ModelState.IsValid)
            {
                IEnumerable<Product> products;

                switch (_search.SelectedOption)
                {
                    case "1":
                        products = ProductsRepository.Products
                            .Where(p => p.ProductPrice >= 1000000 && p.ProductPrice <= 5000000);
                        break;
                    case "2":
                        products = ProductsRepository.Products
                            .Where(p => p.ProductPrice >= 6000000 && p.ProductPrice <= 10000000);
                        break;
                    case "3":
                        products = ProductsRepository.Products
                            .Where(p => p.ProductPrice >= 11000000 && p.ProductPrice <= 15000000);
                        break;
                    case "4":
                        products = ProductsRepository.Products
                            .Where(p => p.ProductPrice >= 16000000 && p.ProductPrice <= 20000000);
                        break;
                    default:
                        products = ProductsRepository.Products
                            .Take(1);
                        break;
                }
                ProductsListViewModel model = new ProductsListViewModel
                {
                    Products = products
                        .OrderBy(e => e.ProductID)
                        .Skip((page - 1) * PageSize)
                        .Take(PageSize),
                    pagingInfo = new PagingInfo
                    {
                        CurrentPage = page,
                        ItemsPerPage = PageSize,
                        TotalItems = products.Count()
                    }
                };
                ViewBag.a = _search.SelectedOption;
                return View("Result", model);
            }

            return View();
        }
    }
}
